package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.RoleDao;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Role;
import com.woniuxy.entity.RoleMenuDTO;
import com.woniuxy.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired(required = false)
    RoleDao roleDao;

    @Override
    public ResponseEntity findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Role> list = roleDao.findAll();
        PageInfo<Role> pageInfo = PageInfo.of(list);
        return new ResponseEntity(200,"查询角色成功",pageInfo);
    }


    @Override
    public ResponseEntity addOne(Role role) {
        if(roleDao.addOne(role) > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity updateOne(Role role) {
        if(roleDao.updateOne(role) > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        if(roleDao.deleteOne(id) > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity findByContext(Role role) {
        List<Role> list = roleDao.findByContext(role);
        return new ResponseEntity(200,"查询指定内容成功",list);
    }

    @Override
    public ResponseEntity checkedRole(int roleId) {
        List<Integer> checked = roleDao.checkedRole(roleId);
        return new ResponseEntity<>(200,"执行成功",checked);
    }

    @Override
    @Transactional
    public ResponseEntity changeMenus(RoleMenuDTO roleMenuDTO) {
        int i = roleDao.delete(roleMenuDTO.getRoleId());
        int j = roleDao.saveChangeMenus(roleMenuDTO.getRoleId(),roleMenuDTO.getModuleIds());
        if (j>0){
            return new ResponseEntity<>(200,"成功更新权限菜单数据",null);
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity batchDelete(int[] ids) {
        if(roleDao.batchDelete(ids) > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public List<String> findByids(String[] ids) {
        System.out.println("获得角色名称集合："+roleDao.findByids(ids));
        return roleDao.findByids(ids);
    }

    @Override
    public ResponseEntity findAll() {
        return new ResponseEntity(200,"查询所有角色成功",roleDao.findAll());
    }

    @Override
    public ResponseEntity getRoles(int id) {
        String roles = roleDao.getRoles(id);
        System.out.println("roles:"+roles);
        if(roles == null){
            return new ResponseEntity<>(500,"角色为空",null);
        }else{
            String[] rolesArr = roles.split(",");
            return new ResponseEntity<>(200,"查询用户角色成功",rolesArr);
        }
    }

    @Override
    public ResponseEntity updateRoles(int id , String roles) {
        if(roleDao.updateRoles(id,roles) > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

}
